package cyou.breathe.blog.folder.facade;

import cyou.breathe.blog.api.folder.model.FolderVO;
import cyou.breathe.blog.api.folder.request.*;
import cyou.breathe.blog.api.folder.response.*;
import cyou.breathe.blog.api.folder.service.FolderFacadeService;
import cyou.breathe.blog.folder.domain.entity.Folder;
import cyou.breathe.blog.folder.domain.entity.convertor.FolderConvertor;
import cyou.breathe.blog.folder.domain.service.FolderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-05
 */
@DubboService(version = "1.0.0")
public class FolderFacadeServiceImpl implements FolderFacadeService {

    @Autowired
    private FolderService folderService;

    @Override
    public FolderCreateDirectoryResponse createDirectory(FolderCreateDirectoryRequest request) {
        return folderService.createDirectory(request);
    }

    @Override
    public FolderUpdateResponse updateFolder(FolderUpdateRequest request) {
        return folderService.updateFolder(request);
    }

    @Override
    public FolderCreateRootResponse createRootFolder(FolderCreateRootRequest request) {
        return folderService.createRootFolder(request);
    }

    @Override
    public FolderDeleteResponse deleteFolder(FolderDeleteRequest request) {
        return folderService.deleteFolder(request);
    }

    /**
     * 获取该文件下的所有文件夹，不包含文章
     * @param request
     * @return
     */
    @Override
    public FolderQueryResponse<FolderVO> getFolderById(FolderQueryRequest request) {
        FolderQueryResponse<FolderVO> response = new FolderQueryResponse<>();

        Folder parent = folderService.getFolderById(request.getFolderId(), request.getUserId());

        List<Folder> sons = folderService.getFolderByParentId(request.getFolderId(), request.getUserId());

        parent.setChildren(sons);

        FolderVO folderVO = FolderConvertor.INSTANCE.mapToVO(parent);

        response.setData(folderVO);
        response.setSuccess(true);
        return response;
    }

    @Override
    public FolderQueryResponse<List<FolderVO>> getFolderByParentId(Long folderId, Long userId) {
        FolderQueryResponse<List<FolderVO>> response = new FolderQueryResponse<>();
        List<Folder> folderByParentId = folderService.getFolderByParentId(folderId, userId);
        List<FolderVO> result = folderByParentId.stream()
                .map(FolderConvertor.INSTANCE::mapToVO)
                .toList();

        response.setSuccess(true);
        response.setData(result);

        return response;
    }

}
